package com.github.llyb120.performance.test

class All{
    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList()
        }
    }

    static Generate generate(Closure closure){
        def _generate = new Generate()
        pushStack(_generate)
        closure.call()
        return _generate
    }

    static Field field(Closure closure){
        def _field = new Field()
        def item = pickStack()
        if(item instanceof Generate){
            item.getField().add(_field)
        }
        pushStack(_field)
        closure.call()
        popStack()
        return _field
    }

    static Field field(String name,Closure closure = null){
        def _field = new Field()
        _field.setName(name)
        def item = pickStack()
        if(item instanceof Generate){
            item.getField().add(_field)
        }
        pushStack(_field)
        if(closure != null){
            closure.call()
        }
        popStack()
        return _field
    }

    static void name(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setName(val)
        }
    }

    static void generator(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setGenerator(val)
        }
    }

    static void params(Map val){
        def item = pickStack()
        if(item instanceof Field){
            item.setParams(val)
        }
    }

    private final static Object pickStack(){
        return local.get().getLast()
    }
    private final static Object pushStack(obj){
        local.get().addLast(obj)
    }
    private final static Object popStack(){
        return local.get().removeLast()
    }
    public final static List getElements(){
        return (List) local.get()
    }
}